package util;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class SessionsUtil {
	private static Map<String,Map<String,String>> sessions = new ConcurrentHashMap<String,Map<String,String>>();
	
	public static String getAtrribute(String JSESSIONID, String param) {
		Map<String,String> sessionMap = sessions.get(JSESSIONID);
		if(sessionMap == null){
			return null;
		} else {
			return sessionMap.get(param);
		}
	}
	
	public static void setAtrribute(String JSESSIONID, String param, String value) {
		Map<String, String> sessionMap = null;
		if(sessions.containsKey(JSESSIONID)) {
			sessionMap = sessions.get(JSESSIONID);
			sessionMap.put(param, value);
		} else {
			sessionMap = new ConcurrentHashMap<String, String>();
			sessionMap.put(param, value);
			sessions.put(JSESSIONID, sessionMap);
		}
	}
	public static void setTimeout(String JSESSIONID,int timeout){
		Timer timer = new Timer();
		final String id = JSESSIONID;
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				sessions.remove(id);
			}
		}, 1000 * timeout);
	}
}
